#include <stdio.h>
#include <malloc.h>
#include <string.h>
void judgeGoldBach(int begin, int end);
void square(int odd);
void sort(int array[], int size);
int main(void)
{
    judgeGoldBach(4, 50);
    square(5);
    int a[5] = {2, 5, 7, 3, 1};
    sort(a, 5);
    return 0;
}
void judgeGoldBach(int begin, int end)
{
    for (int num = begin; num <= end; num += 2)
    {
        for (int fir = 2; fir < num; ++fir)
        {
            bool fir_isprime = true;
            for (int i = 2; i * i <= fir; ++i) // judge fir is prime
            {
                if (!(fir % i))
                {
                    fir_isprime = false;
                    break;
                }
            }
            if (!fir_isprime)
                continue;
            bool sec_isprime = true;
            int sec = num - fir; // get sec by fir
            for (int i = 2; i * i <= sec; ++i) // judge sec is prime
            {
                if (!(sec % i))
                {
                    sec_isprime = false;
                    break;
                }
            }
            if (sec_isprime)
            {
                printf("%d = %d + %d\n", num, fir, sec);
                break;
            }  
        }
    }
    printf("\n");
}
void square(int odd)
{
    int** s = (int**)malloc(sizeof(int*) * odd);
    for (int i = 0; i < odd; ++i)
    {
        s[i] = (int*)malloc(sizeof(int) * odd);
        memset(s[i], 0, sizeof(int) * odd);
    }
    int r = 0, c = odd / 2;
    s[r][c] = 1;
    for (int n = 2; n <= odd * odd; ++n)
    {
        --r, ++c;
        if (r >= 0 && c < odd) // 没出格
        {
            if (s[r][c]) // 位置被占用
            {
                --c;
                r += 2;
            }
        }
        else if (r < 0 && c < odd) // 行出格
            r = odd - 1;
        else if (r >= 0 && c >= odd) // 列出格
            c = 0;
        else // 全出格
        {
            --c;
            r += 2;
        }
        s[r][c] = n;
    }
    for (int i = 0; i < odd; ++i)
    {
        for (int j = 0; j < odd; ++j)
            printf("%3d", s[i][j]);
        printf("\n");
    }
    printf("\n");
}
void sort(int array[], int size)
{
    for (int i = 1; i < size; ++i)
    {
        int j, cur = array[i];
        for (j = i - 1; j >= 0; --j)
        {
            if (cur > array[j])
                break;
            array[j+1] = array[j];
        }
        array[j+1] = cur;        
    }
    for (int i = 0; i < size; ++i)
        printf("%2d", array[i]);
    printf("\n");
}